<?php
/*
 *   This file is part of Verbum.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * 	 Copyright (c) 2008 Sergio Gabriel Teves <gabriel.sgt at gmail.com>
 */

require_once("entity.class.php");

class credentials extends entity {

	var $id;
	var $groupid;
	var $actionid;
	var $opid;

	public function __construct()
   {
   }

	// THIS CREATE A NEW INSTANCE OF THIS CLASS
	public function newInstance() {
		return new credentials;
	}

	// COMMON PUBLIC FUNCTIONS // NEEDED FOR LOADER
	public function get() {
		$sql = "SELECT * FROM ".CREDENTIALS." WHERE groupid = ".util::str($this->groupid)." AND actionid = ".util::str($this->actionid)." AND opid = ".util::str($this->opid);
		return $sql;
	}

	public function listAll() {
		$sql = "";
		return $sql;
	}

	public static function listByGroup($grp,$actId) {
		if (isset($actId)) {
			$sql = "SELECT * FROM ".CREDENTIALS." WHERE groupid = ".util::str($grp)." AND actionid = ".util::str($actId);
		} else {
			$sql = "SELECT * FROM ".CREDENTIALS." WHERE groupid = ".util::str($grp);	
		}
		return $sql;
	}
	
	public function insert() {
		$sql = "INSERT INTO ".CREDENTIALS." (groupid,actionid,opid) VALUES (";
		$sql.= util::str($this->groupid).",";
		$sql.= util::str($this->actionid).",";
		$sql.= util::str($this->opid).")";
		return $sql;
	}

	public function update() {
		$sql = "";
		return $sql;
	}

	public function delete() {
		$sql = "DELETE FROM ".CREDENTIALS." WHERE id = ".util::str($this->id);
		return $sql;
	}
	
	public static function exists($groupid,$actionid,$opid) {
		$sql = "SELECT 1 FROM ".CREDENTIALS." WHERE groupid = ".util::str($groupid)." AND ((actionid = ".util::str($actionid);
		if (isset($opid) && $opid!="") {
			$sql .= " AND (opid = ".util::str($opid)." OR opid = '*')";
		}
		$sql .=") OR actionid = '*')";
		return $sql;
	}
	
	public static function contains($list,$actionid,$op=null) {
		global $_log;
		$rs = false;
		$_log->debug("[contains] actionid: ".$actionid);
		if (inArray($list,'actionid',"*")===false) {
			if ($op==null) {
				if (inArray($list,'actionid',$actionid)!==false) {
					$rs = true;
				}		
			} else {
				$_log->debug("[contains] opid: ".$op);
				$r = inArray($list,'actionid',$actionid,'opid',$op);
				if ($r!==false) {
					$rs = true;
				}
			}
		} else {
			$rs = true;
		}
		$_log->debug("[contains] contains: ".$rs);
		return $rs;
	}
	
	public static function groupCopy($source, $target) {
		$sql = "INSERT INTO ".CREDENTIALS." (groupid, actionid, opid) (SELECT ".util::str($target).",actionid,opid FROM ".CREDENTIALS." WHERE groupid = ".util::str($source).")";
		return $sql;	
	}
	
	public static function clearGroup($grp) {
		$sql = "DELETE FROM ".CREDENTIALS." WHERE groupid = ".util::str($grp);
		return $sql;	
	}
	
	public function fill($array) {
		$this->id=$array['id'];
		$this->groupid=$array['groupid'];
		$this->actionid=$array['actionid'];
		$this->opid=$array['opid'];
	}

}

?>
